//
//  Tool.m
//  ivcs_ios_demo
//
//  Created by zealcomm user on 2020/10/23.
//  Copyright © 2020 zealcomm user. All rights reserved.
//

#import "Tool.h"
#import <objc/runtime.h>
@implementation Tool
+(NSString*)getCurrentTimes
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *datenow = [NSDate date];
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    return currentTimeString;
}

+(NSString*)getCurrentTimeMillions{
    NSDate  *dateValue = [NSDate date];
    NSTimeInterval second = [dateValue timeIntervalSince1970];
    long  long dTime = [[NSNumber numberWithDouble:second*1000] longLongValue];
    return [NSString stringWithFormat:@"%llu",dTime];
}

+(BOOL)keyInNSDictionary:(NSDictionary *)dic withKey:(nonnull NSString *)key{
    if ([[dic allKeys] containsObject:key]) {
        return true;
    }
    return false;
}

+ (BOOL)changeValue:(NSObject *)obj withKey:(NSString *)key andValue:(id)value{
    //记录属性的个数
    unsigned int count = 0;
    //获取属性列表
    Ivar *members = class_copyIvarList([obj class], &count);
    //遍历属性列表
    for (NSInteger i = 0; i < count; i++) {
        //取到属性
        Ivar ivar = members[i];
        //获取属性名
        const char *memberName = ivar_getName(ivar);
        NSString *ivarName = [NSString stringWithFormat:@"%s", memberName];
        //当属性名是 key 的时候对其值进行修改
        if ([ivarName isEqualToString:key]) {
            object_setIvar(obj, ivar, value);
            return true;
        }
    }
    return false;
}

@end
